package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.contast.MessageConstant;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gallant
 * @create 2020-07-03 18:55
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                // 去数据库中查看这个日期的数据是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                // 对count进行判断
                if (count > 0) {
                    // 说明数据库里边已经有数据了,那就执行更新操作
                    orderSettingDao.updateNumberByOrderDate(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        // 首先设置要去数据库查询前的参数的形式
        String beginDate = date + "-1";
        String endDate = date + "-31";
        // 然后将查询的条件封装成一个map
        Map map = new HashMap();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        // 调用dao获取查询到的ordersetting
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        // 然后对list进行封装
        List<Map> data = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map m = new HashMap();
                m.put("date", orderSetting.getOrderDate().getDate());
                m.put("number", orderSetting.getNumber());
                m.put("reservations", orderSetting.getReservations());
                data.add(m);
            }
        }
        return data;
    }

    // 根据日期设置预约数量
    public void  editNumberByDate(OrderSetting orderSetting){
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            orderSettingDao.editNumberByDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
